package com.alliky.core.base

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.alliky.core.R
import com.alliky.core.callback.OnItemClickListener

/**
 * ================================================
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/11/24 16:14
 * ================================================
 */
abstract class BasePagingAdapter<T : Any>(differCallback: DiffUtil.ItemCallback<T>) :
    PagingDataAdapter<T, RecyclerView.ViewHolder>(differCallback) {

    companion object {
        const val TAG = "BasePagingAdapter"
    }

    private var mListener: OnItemClickListener<T>? = null

    open fun setOnItemClickListener(listener: OnItemClickListener<T>) {
        this.mListener = listener
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position) ?: return
        (holder as BasePagingAdapter<*>.BaseViewHolder).onBindData(item)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = BaseViewHolder(parent, viewType)
        //Item的点击事件
        holder.itemView.setOnClickListener {
//            onItemClick(it, getItem(holder.layoutPosition), holder.layoutPosition)
            mListener?.onItemClick(it, getItem(holder.layoutPosition), holder.layoutPosition)
        }
        return holder
    }

    override fun getItemViewType(position: Int): Int {
        return getItemLayout(position)
    }

    /**
     * 子类获取layout
     */
    protected abstract fun getItemLayout(position: Int): Int

    /**
     * 子类绑定数据
     */
    protected abstract fun convert(holder: OnBindViewHolder, item: T?)

    inner class BaseViewHolder(parent: ViewGroup, layout: Int) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(layout, parent, false)
    ) {
        private val helper: OnBindViewHolder = OnBindViewHolder(this)

        fun onBindData(item: Any?) {
            convert(helper, item as T)
        }
    }

    /**
     * ItemView的辅助类
     */
    class OnBindViewHolder(holder: BasePagingAdapter<*>.BaseViewHolder) {
        private val itemView = holder.itemView
        private val viewCache = SparseArray<View>()

        private fun findViewById(viewId: Int): View {
            var view = viewCache.get(viewId)
            if (view == null) {
                view = itemView.findViewById(viewId)
                if (view == null) {
                    throw NullPointerException("$viewId can not find this item！")
                }
                viewCache.put(viewId, view)
            }
            return view
        }

        /**
         * TextView设置内容
         */
        fun setText(viewId: Int, text: CharSequence?): OnBindViewHolder {
            (findViewById(viewId) as TextView).text = text
            return this
        }

        /**
         * Coil加载图片
         */
        fun setImage(viewId: Int, url: String?): OnBindViewHolder {
            val imageView: ImageView = findViewById(viewId) as ImageView
            imageView.load(url) {
                placeholder(R.mipmap.icon_default)
            }
            return this
        }
    }
}